package com.bst.bsj.portal.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component(value = "beanLocator")
public class BeanLocator implements ApplicationContextAware, InitializingBean {

	private static ApplicationContext applicationContext;
	//
	private static volatile boolean initialized = false;

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		BeanLocator.applicationContext = applicationContext;
		initialized = true;
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(BeanLocator.applicationContext,
				"BeanLocator not initialized properly yet");
	}

	public static Object getBean(String name) {
		return BeanLocator.applicationContext.getBean(name);
	}

	public static boolean isInitialized() {
		return initialized;
	}

	public static <T extends Object> T getBean(String name, Class<T> clz) {
		return BeanLocator.applicationContext.getBean(name, clz);
	}
}
